package com.yidu.Util.util;

import java.sql.*;

public class MySqlUtil {
    /**
     * 获得连接对象
     * @return 连接对象
     */
    public static Connection getCon()
    {
        Connection connection=null;
        //第一步加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //第二步获得连接
            String url="jdbc:mysql://localhost:3306/finance";
            String userName="root";
            String userPwd="123456";
            connection= DriverManager.getConnection(url, userName, userPwd);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }


    /**
     * 释放资源
     * @param rs 结果集
     * @param statement 语句对象
     * @param con 连接对象
     */
    public static void closeAll(ResultSet rs, Statement statement, Connection con){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con!=null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

}
